home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Memory / PlayForm.frm < prev    next >
Text File  |  2001-10-08  |  34KB  |  1,006 lines

  1. VERSION 5.00
  2. Begin VB.Form frmGameBoard 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "DirectPlay Memory"
  5.    ClientHeight    =   7200
  6.    ClientLeft      =   3150
  7.    ClientTop       =   2400
  8.    ClientWidth     =   8745
  9.    Icon            =   "PlayForm.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    ScaleHeight     =   480
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   583
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.Timer tmrTerminate 
  17.       Enabled         =   0   'False
  18.       Interval        =   10
  19.       Left            =   8985
  20.       Top             =   1680
  21.    End
  22.    Begin VB.Timer tmrResign 
  23.       Enabled         =   0   'False
  24.       Interval        =   10
  25.       Left            =   8985
  26.       Top             =   1200
  27.    End
  28.    Begin VB.CommandButton cmdExit 
  29.       Cancel          =   -1  'True
  30.       Caption         =   "E&xit"
  31.       BeginProperty Font 
  32.          Name            =   "Verdana"
  33.          Size            =   9.75
  34.          Charset         =   0
  35.          Weight          =   700
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   615
  41.       Left            =   6720
  42.       TabIndex        =   9
  43.       Top             =   1740
  44.       Visible         =   0   'False
  45.       Width           =   1995
  46.    End
  47.    Begin VB.Frame Frame1 
  48.       BeginProperty Font 
  49.          Name            =   "Verdana"
  50.          Size            =   9.75
  51.          Charset         =   0
  52.          Weight          =   700
  53.          Underline       =   0   'False
  54.          Italic          =   0   'False
  55.          Strikethrough   =   0   'False
  56.       EndProperty
  57.       Height          =   1455
  58.       Index           =   1
  59.       Left            =   6720
  60.       TabIndex        =   3
  61.       Top             =   1760
  62.       Width           =   1935
  63.       Begin VB.Label LabelScore 
  64.          Alignment       =   2  'Center
  65.          Caption         =   "0"
  66.          BeginProperty Font 
  67.             Name            =   "Verdana"
  68.             Size            =   36
  69.             Charset         =   0
  70.             Weight          =   700
  71.             Underline       =   0   'False
  72.             Italic          =   0   'False
  73.             Strikethrough   =   0   'False
  74.          EndProperty
  75.          Height          =   975
  76.          Index           =   1
  77.          Left            =   120
  78.          TabIndex        =   5
  79.          Top             =   360
  80.          Width           =   1695
  81.       End
  82.    End
  83.    Begin VB.Frame Frame1 
  84.       BeginProperty Font 
  85.          Name            =   "Verdana"
  86.          Size            =   9.75
  87.          Charset         =   0
  88.          Weight          =   700
  89.          Underline       =   0   'False
  90.          Italic          =   0   'False
  91.          Strikethrough   =   0   'False
  92.       EndProperty
  93.       Height          =   1455
  94.       Index           =   2
  95.       Left            =   6720
  96.       TabIndex        =   2
  97.       Top             =   3400
  98.       Width           =   1935
  99.       Begin VB.Label LabelScore 
  100.          Alignment       =   2  'Center
  101.          Caption         =   "0"
  102.          BeginProperty Font 
  103.             Name            =   "Verdana"
  104.             Size            =   36
  105.             Charset         =   0
  106.             Weight          =   700
  107.             Underline       =   0   'False
  108.             Italic          =   0   'False
  109.             Strikethrough   =   0   'False
  110.          EndProperty
  111.          Height          =   975
  112.          Index           =   2
  113.          Left            =   120
  114.          TabIndex        =   6
  115.          Top             =   360
  116.          Width           =   1695
  117.       End
  118.    End
  119.    Begin VB.Frame Frame1 
  120.       BeginProperty Font 
  121.          Name            =   "Verdana"
  122.          Size            =   9.75
  123.          Charset         =   0
  124.          Weight          =   700
  125.          Underline       =   0   'False
  126.          Italic          =   0   'False
  127.          Strikethrough   =   0   'False
  128.       EndProperty
  129.       Height          =   1455
  130.       Index           =   3
  131.       Left            =   6720
  132.       TabIndex        =   1
  133.       Top             =   5040
  134.       Width           =   1935
  135.       Begin VB.Label LabelScore 
  136.          Alignment       =   2  'Center
  137.          Caption         =   "0"
  138.          BeginProperty Font 
  139.             Name            =   "Verdana"
  140.             Size            =   36
  141.             Charset         =   0
  142.             Weight          =   700
  143.             Underline       =   0   'False
  144.             Italic          =   0   'False
  145.             Strikethrough   =   0   'False
  146.          EndProperty
  147.          Height          =   975
  148.          Index           =   3
  149.          Left            =   120
  150.          TabIndex        =   7
  151.          Top             =   360
  152.          Width           =   1695
  153.       End
  154.    End
  155.    Begin VB.Frame Frame1 
  156.       Caption         =   "Turns"
  157.       BeginProperty Font 
  158.          Name            =   "Verdana"
  159.          Size            =   9.75
  160.          Charset         =   0
  161.          Weight          =   700
  162.          Underline       =   0   'False
  163.          Italic          =   0   'False
  164.          Strikethrough   =   0   'False
  165.       EndProperty
  166.       Height          =   1455
  167.       Index           =   0
  168.       Left            =   6720
  169.       TabIndex        =   0
  170.       Top             =   120
  171.       Width           =   1935
  172.       Begin VB.Label LabelScore 
  173.          Alignment       =   2  'Center
  174.          Caption         =   "0"
  175.          BeginProperty Font 
  176.             Name            =   "Verdana"
  177.             Size            =   36
  178.             Charset         =   0
  179.             Weight          =   700
  180.             Underline       =   0   'False
  181.             Italic          =   0   'False
  182.             Strikethrough   =   0   'False
  183.          EndProperty
  184.          Height          =   975
  185.          Index           =   0
  186.          Left            =   120
  187.          TabIndex        =   4
  188.          Top             =   360
  189.          Width           =   1695
  190.       End
  191.    End
  192.    Begin VB.Label lblChat 
  193.       Caption         =   "Press Enter to chat, Alt+F4 to resign."
  194.       BeginProperty Font 
  195.          Name            =   "Verdana"
  196.          Size            =   9.75
  197.          Charset         =   0
  198.          Weight          =   400
  199.          Underline       =   0   'False
  200.          Italic          =   0   'False
  201.          Strikethrough   =   0   'False
  202.       EndProperty
  203.       Height          =   570
  204.       Left            =   105
  205.       TabIndex        =   8
  206.       Top             =   6570
  207.       Width           =   8700
  208.    End
  209.    Begin VB.Image Image1 
  210.       BorderStyle     =   1  'Fixed Single
  211.       Height          =   1005
  212.       Index           =   35
  213.       Left            =   5520
  214.       Stretch         =   -1  'True
  215.       Top             =   5520
  216.       Width           =   1005
  217.    End
  218.    Begin VB.Image Image1 
  219.       BorderStyle     =   1  'Fixed Single
  220.       Height          =   1005
  221.       Index           =   34
  222.       Left            =   4440
  223.       Stretch         =   -1  'True
  224.       Top             =   5520
  225.       Width           =   1005
  226.    End
  227.    Begin VB.Image Image1 
  228.       BorderStyle     =   1  'Fixed Single
  229.       Height          =   1005
  230.       Index           =   33
  231.       Left            =   3360
  232.       Stretch         =   -1  'True
  233.       Top             =   5520
  234.       Width           =   1005
  235.    End
  236.    Begin VB.Image Image1 
  237.       BorderStyle     =   1  'Fixed Single
  238.       Height          =   1005
  239.       Index           =   32
  240.       Left            =   2280
  241.       Stretch         =   -1  'True
  242.       Top             =   5520
  243.       Width           =   1005
  244.    End
  245.    Begin VB.Image Image1 
  246.       BorderStyle     =   1  'Fixed Single
  247.       Height          =   1005
  248.       Index           =   31
  249.       Left            =   1200
  250.       Stretch         =   -1  'True
  251.       Top             =   5520
  252.       Width           =   1005
  253.    End
  254.    Begin VB.Image Image1 
  255.       BorderStyle     =   1  'Fixed Single
  256.       Height          =   1005
  257.       Index           =   30
  258.       Left            =   120
  259.       Stretch         =   -1  'True
  260.       Top             =   5520
  261.       Width           =   1005
  262.    End
  263.    Begin VB.Image Image1 
  264.       BorderStyle     =   1  'Fixed Single
  265.       Height          =   1005
  266.       Index           =   29
  267.       Left            =   5520
  268.       Stretch         =   -1  'True
  269.       Top             =   4440
  270.       Width           =   1005
  271.    End
  272.    Begin VB.Image Image1 
  273.       BorderStyle     =   1  'Fixed Single
  274.       Height          =   1005
  275.       Index           =   28
  276.       Left            =   4440
  277.       Stretch         =   -1  'True
  278.       Top             =   4440
  279.       Width           =   1005
  280.    End
  281.    Begin VB.Image Image1 
  282.       BorderStyle     =   1  'Fixed Single
  283.       Height          =   1005
  284.       Index           =   27
  285.       Left            =   3360
  286.       Stretch         =   -1  'True
  287.       Top             =   4440
  288.       Width           =   1005
  289.    End
  290.    Begin VB.Image Image1 
  291.       BorderStyle     =   1  'Fixed Single
  292.       Height          =   1005
  293.       Index           =   26
  294.       Left            =   2280
  295.       Stretch         =   -1  'True
  296.       Top             =   4440
  297.       Width           =   1005
  298.    End
  299.    Begin VB.Image Image1 
  300.       BorderStyle     =   1  'Fixed Single
  301.       Height          =   1005
  302.       Index           =   25
  303.       Left            =   1200
  304.       Stretch         =   -1  'True
  305.       Top             =   4440
  306.       Width           =   1005
  307.    End
  308.    Begin VB.Image Image1 
  309.       BorderStyle     =   1  'Fixed Single
  310.       Height          =   1005
  311.       Index           =   24
  312.       Left            =   120
  313.       Stretch         =   -1  'True
  314.       Top             =   4440
  315.       Width           =   1005
  316.    End
  317.    Begin VB.Image Image1 
  318.       BorderStyle     =   1  'Fixed Single
  319.       Height          =   1005
  320.       Index           =   23
  321.       Left            =   5520
  322.       Stretch         =   -1  'True
  323.       Top             =   3360
  324.       Width           =   1005
  325.    End
  326.    Begin VB.Image Image1 
  327.       BorderStyle     =   1  'Fixed Single
  328.       Height          =   1005
  329.       Index           =   22
  330.       Left            =   4440
  331.       Stretch         =   -1  'True
  332.       Top             =   3360
  333.       Width           =   1005
  334.    End
  335.    Begin VB.Image Image1 
  336.       BorderStyle     =   1  'Fixed Single
  337.       Height          =   1005
  338.       Index           =   21
  339.       Left            =   3360
  340.       Stretch         =   -1  'True
  341.       Top             =   3360
  342.       Width           =   1005
  343.    End
  344.    Begin VB.Image Image1 
  345.       BorderStyle     =   1  'Fixed Single
  346.       Height          =   1005
  347.       Index           =   20
  348.       Left            =   2280
  349.       Stretch         =   -1  'True
  350.       Top             =   3360
  351.       Width           =   1005
  352.    End
  353.    Begin VB.Image Image1 
  354.       BorderStyle     =   1  'Fixed Single
  355.       Height          =   1005
  356.       Index           =   19
  357.       Left            =   1200
  358.       Stretch         =   -1  'True
  359.       Top             =   3360
  360.       Width           =   1005
  361.    End
  362.    Begin VB.Image Image1 
  363.       BorderStyle     =   1  'Fixed Single
  364.       Height          =   1005
  365.       Index           =   18
  366.       Left            =   120
  367.       Stretch         =   -1  'True
  368.       Top             =   3360
  369.       Width           =   1005
  370.    End
  371.    Begin VB.Image Image1 
  372.       BorderStyle     =   1  'Fixed Single
  373.       Height          =   1005
  374.       Index           =   17
  375.       Left            =   5520
  376.       Stretch         =   -1  'True
  377.       Top             =   2280
  378.       Width           =   1005
  379.    End
  380.    Begin VB.Image Image1 
  381.       BorderStyle     =   1  'Fixed Single
  382.       Height          =   1005
  383.       Index           =   16
  384.       Left            =   4440
  385.       Stretch         =   -1  'True
  386.       Top             =   2280
  387.       Width           =   1005
  388.    End
  389.    Begin VB.Image Image1 
  390.       BorderStyle     =   1  'Fixed Single
  391.       Height          =   1005
  392.       Index           =   15
  393.       Left            =   3360
  394.       Stretch         =   -1  'True
  395.       Top             =   2280
  396.       Width           =   1005
  397.    End
  398.    Begin VB.Image Image1 
  399.       BorderStyle     =   1  'Fixed Single
  400.       Height          =   1005
  401.       Index           =   14
  402.       Left            =   2280
  403.       Stretch         =   -1  'True
  404.       Top             =   2280
  405.       Width           =   1005
  406.    End
  407.    Begin VB.Image Image1 
  408.       BorderStyle     =   1  'Fixed Single
  409.       Height          =   1005
  410.       Index           =   13
  411.       Left            =   1200
  412.       Stretch         =   -1  'True
  413.       Top             =   2280
  414.       Width           =   1005
  415.    End
  416.    Begin VB.Image Image1 
  417.       BorderStyle     =   1  'Fixed Single
  418.       Height          =   1005
  419.       Index           =   12
  420.       Left            =   120
  421.       Stretch         =   -1  'True
  422.       Top             =   2280
  423.       Width           =   1005
  424.    End
  425.    Begin VB.Image Image1 
  426.       BorderStyle     =   1  'Fixed Single
  427.       Height          =   1005
  428.       Index           =   11
  429.       Left            =   5520
  430.       Stretch         =   -1  'True
  431.       Top             =   1200
  432.       Width           =   1005
  433.    End
  434.    Begin VB.Image Image1 
  435.       BorderStyle     =   1  'Fixed Single
  436.       Height          =   1005
  437.       Index           =   10
  438.       Left            =   4440
  439.       Stretch         =   -1  'True
  440.       Top             =   1200
  441.       Width           =   1005
  442.    End
  443.    Begin VB.Image Image1 
  444.       BorderStyle     =   1  'Fixed Single
  445.       Height          =   1005
  446.       Index           =   9
  447.       Left            =   3360
  448.       Stretch         =   -1  'True
  449.       Top             =   1200
  450.       Width           =   1005
  451.    End
  452.    Begin VB.Image Image1 
  453.       BorderStyle     =   1  'Fixed Single
  454.       Height          =   1005
  455.       Index           =   8
  456.       Left            =   2280
  457.       Stretch         =   -1  'True
  458.       Top             =   1200
  459.       Width           =   1005
  460.    End
  461.    Begin VB.Image Image1 
  462.       BorderStyle     =   1  'Fixed Single
  463.       Height          =   1005
  464.       Index           =   7
  465.       Left            =   1200
  466.       Stretch         =   -1  'True
  467.       Top             =   1200
  468.       Width           =   1005
  469.    End
  470.    Begin VB.Image Image1 
  471.       BorderStyle     =   1  'Fixed Single
  472.       Height          =   1005
  473.       Index           =   6
  474.       Left            =   120
  475.       Stretch         =   -1  'True
  476.       Top             =   1200
  477.       Width           =   1005
  478.    End
  479.    Begin VB.Image Image1 
  480.       BorderStyle     =   1  'Fixed Single
  481.       Height          =   1005
  482.       Index           =   5
  483.       Left            =   5520
  484.       Stretch         =   -1  'True
  485.       Top             =   120
  486.       Width           =   1005
  487.    End
  488.    Begin VB.Image Image1 
  489.       BorderStyle     =   1  'Fixed Single
  490.       Height          =   1005
  491.       Index           =   4
  492.       Left            =   4440
  493.       Stretch         =   -1  'True
  494.       Top             =   120
  495.       Width           =   1005
  496.    End
  497.    Begin VB.Image Image1 
  498.       BorderStyle     =   1  'Fixed Single
  499.       Height          =   1005
  500.       Index           =   3
  501.       Left            =   3360
  502.       Stretch         =   -1  'True
  503.       Top             =   120
  504.       Width           =   1005
  505.    End
  506.    Begin VB.Image Image1 
  507.       BorderStyle     =   1  'Fixed Single
  508.       Height          =   1005
  509.       Index           =   2
  510.       Left            =   2280
  511.       Stretch         =   -1  'True
  512.       Top             =   120
  513.       Width           =   1005
  514.    End
  515.    Begin VB.Image Image1 
  516.       BorderStyle     =   1  'Fixed Single
  517.       Height          =   1005
  518.       Index           =   1
  519.       Left            =   1200
  520.       Stretch         =   -1  'True
  521.       Top             =   120
  522.       Width           =   1005
  523.    End
  524.    Begin VB.Image Image1 
  525.       BorderStyle     =   1  'Fixed Single
  526.       Height          =   1005
  527.       Index           =   0
  528.       Left            =   120
  529.       Stretch         =   -1  'True
  530.       Top             =   120
  531.       Width           =   1005
  532.    End
  533. End
  534. Attribute VB_Name = "frmGameBoard"
  535. Attribute VB_GlobalNameSpace = False
  536. Attribute VB_Creatable = False
  537. Attribute VB_PredeclaredId = True
  538. Attribute VB_Exposed = False
  539. Option Explicit
  540. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  541. '
  542. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  543. '
  544. '  File:       PlayForm.frm
  545. '
  546. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  547. Implements DirectPlay8Event
  548. 'Here is where all of the main gameplay will be taking place.
  549. Private Const mlMaxText As Long = 50
  550.  
  551. 'Keep track of what the first cell picked was
  552. Private fFirstPick As Boolean
  553. Private lFirstCell As Long
  554. Private fGame As Boolean
  555. Private lTurnCount As Long
  556. Private mfResign As Boolean
  557. Private mlTerminateCode As Long
  558.  
  559. Private Sub cmdExit_Click()
  560.     'Game over, we wanna leave
  561.     Unload Me
  562. End Sub
  563.  
  564. ' Keystroke handler
  565. ' Enter: open Chat dialog
  566. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  567.     Dim sMsg As String, lOffset As Long
  568.     Dim oBuf() As Byte
  569.     
  570.     If (KeyCode = vbKeyReturn) And (gbNumPlayers > 1) Then
  571.         'Lets chat
  572.         sMsg = InputBox$("Enter the text you want to send:", "Chat Message")
  573.         If sMsg = vbNullString Then Exit Sub
  574.         If Len(sMsg) > mlMaxText Then
  575.             sMsg = Left$(sMsg, mlMaxText)
  576.         End If
  577.         'Send our chat
  578.         lOffset = NewBuffer(oBuf)
  579.         AddDataToBuffer oBuf, CByte(MSG_CHAT), SIZE_BYTE, lOffset
  580.         AddStringToBuffer oBuf, sMsg, lOffset
  581.         SendMessage oBuf
  582.     End If
  583. End Sub
  584.  
  585. Private Sub Form_Load()
  586.  
  587.     ' Initialize scoreboard
  588.     If gbNumPlayers > 1 Then DPlayEventsForm.RegisterCallback Me
  589.     InitLocalGame
  590.     ' Erase chat prompt if only one player.
  591.     If gbNumPlayers = 1 Then
  592.         lblChat.Caption = vbNullString
  593.         cmdExit.Visible = True
  594.         SetupBoard
  595.     Else
  596.         ' Put user name on caption bar to ease debugging of multiple sessions on one machine
  597.         Me.Caption = Me.Caption & " - " & gsUserName
  598.         If gfHost Then Me.Caption = Me.Caption & " (HOST) - Your turn"
  599.     End If
  600. End Sub
  601.  
  602.  
  603. Private Sub Form_Unload(Cancel As Integer)
  604.     mfResign = True
  605.     If Not (DPlayEventsForm Is Nothing) Then DPlayEventsForm.DoSleep 50
  606.     Cleanup
  607.     frmIntro.Visible = True
  608.     frmIntro.EnableButtons True
  609. End Sub
  610.  
  611. ' This is where the action takes place. In each turn the player clicks on two empty squares,
  612. ' making their pictures visible. The two pictures revealed in the previous turn are hidden
  613. ' as soon as the first square is clicked, unless they are a match. The player can click on
  614. ' an unmatched picture to begin the turn, in which case it remains visible.
  615. ' A message is broadcast whenever a square is shown or hidden.
  616.  
  617. Private Sub Image1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  618.   
  619.     Dim fGameOver As Boolean
  620.     Dim lCount As Long, lOffset As Long
  621.     Dim oBuf() As Byte
  622.     
  623.     ' Not your turn, bub.
  624.     If gbNumPlayers > 1 Then If glPlayerIDs(glCurrentPlayer) <> glMyPlayerID Then Exit Sub
  625.     
  626.     If Button = vbLeftButton Then 'Button = Left
  627.         ' If picture already showing and this is second pick, ignore click.
  628.         ' If picture showing and is already one of a match, ignore click.
  629.         If Image1(Index).Picture <> 0 And ((Not fFirstPick) Or gfMatchedCells(Index)) Then
  630.             Exit Sub
  631.         End If
  632.     
  633.         
  634.         If fFirstPick Then ' First Pick
  635.         ' Hide previous picks unless they were a match.
  636.             For lCount = 0 To NumCells - 1
  637.                 If Not gfMatchedCells(lCount) Then 'Not Matched
  638.                     Set Image1(lCount).Picture = Nothing
  639.                 End If 'Not Matched
  640.             Next lCount
  641.             ' Tell the other players to update the display. We don't specify which
  642.             ' squares, but just tell them to hide unmatched squares.
  643.             If gbNumPlayers > 1 Then 'NumPlayers > 1
  644.                 lOffset = NewBuffer(oBuf)
  645.                 AddDataToBuffer oBuf, CByte(MSG_HIDEPIECES), SIZE_BYTE, lOffset
  646.                 SendMessage oBuf
  647.             End If 'NumPlayers > 1
  648.             ' Remember this one
  649.             lFirstCell = Index
  650.             fFirstPick = False
  651.             ShowPic Index
  652.         Else
  653.             ShowPic Index
  654.             ' Second pick
  655.             fFirstPick = True  ' Reset for next time
  656.             ' In solitaire game, show number of turns as score
  657.             If gbNumPlayers = 1 Then '1 Player?
  658.                 lTurnCount = lTurnCount + 1
  659.                 frmGameBoard.LabelScore(0).Caption = lTurnCount
  660.             End If '1 Player?
  661.     
  662.             ' Check for match
  663.             If gbPicArray(lFirstCell) = gbPicArray(Index) Then
  664.                 ' There was a match
  665.                 gfMatchedCells(Index) = True
  666.                 gfMatchedCells(lFirstCell) = True
  667.     
  668.                 ' Check for win and increment score (# of matches)
  669.                 fGameOver = IsGameOver
  670.                 ' Increment score display only in multiplayer.
  671.                 ' For solitaire, the score is the turn count.
  672.                 If gbNumPlayers > 1 Then
  673.                     'Update the scoreboard for multiplayer games
  674.                     UpdateScoreboard
  675.     
  676.                     lOffset = NewBuffer(oBuf)
  677.                     AddDataToBuffer oBuf, CByte(MSG_MATCHED), SIZE_BYTE, lOffset
  678.                     'Get the array of matchings cells in
  679.                     For lCount = 0 To NumCells - 1
  680.                         AddDataToBuffer oBuf, gfMatchedCells(lCount), LenB(gfMatchedCells(lCount)), lOffset
  681.                     Next
  682.                     ' Get scores into message
  683.                     For lCount = 0 To MaxPlayers - 1
  684.                         AddDataToBuffer oBuf, gbPlayerScores(lCount), LenB(gbPlayerScores(lCount)), lOffset
  685.                     Next
  686.                     SendMessage oBuf
  687.                 End If ' DirectPlay exists
  688.             Else
  689.                 ' There was no match.
  690.                 ' Broadcast turn-end message
  691.             
  692.                 If gbNumPlayers > 1 Then
  693.                     lOffset = NewBuffer(oBuf)
  694.                     AddDataToBuffer oBuf, CByte(MSG_TURNEND), SIZE_BYTE, lOffset
  695.                     SendMessage oBuf
  696.             
  697.                     ' Pass control to next player & advance scoreboard highlight
  698.                     AdvanceTurn
  699.                 End If  'More than one player
  700.             
  701.             End If ' match or no match
  702.             
  703.             ' If solitaire win, offer choice to play again
  704.             If fGameOver And gbNumPlayers = 1 Then
  705.                 If MsgBox("Play again?", vbYesNo, "Game Over") = vbNo Then End
  706.                 SetupBoard
  707.                 InitLocalGame
  708.             End If
  709.         End If
  710.     End If
  711.  
  712. End Sub
  713.  
  714.  
  715. ' Update scores and check for win
  716.  
  717. Public Function IsGameOver() As Boolean
  718.     
  719.     Dim lCount As Integer, Response As Integer
  720.     Dim fEnd As Boolean
  721.     
  722.     gbPlayerScores(glCurrentPlayer) = gbPlayerScores(glCurrentPlayer) + 1
  723.     
  724.     ' If any cells are still blank, game is not over
  725.     fEnd = True
  726.     For lCount = 0 To NumCells - 1
  727.         If Not gfMatchedCells(lCount) Then
  728.             fEnd = False
  729.         End If
  730.     Next lCount
  731.     IsGameOver = fEnd
  732.     
  733. End Function
  734.  
  735. ' Game initialization for all players, including setting up the scoreboard for the
  736. ' current number and order of players. Global game initialization (setting up the pieces)
  737. ' is handled by the host through SetupBoard.
  738.  
  739. Public Sub InitLocalGame()
  740.  
  741.     Dim lCount As Integer
  742.     Dim PlayerInfo As DPN_PLAYER_INFO
  743.     
  744.     fFirstPick = True
  745.     lTurnCount = 0
  746.     
  747.     ' Highlight current player
  748.     glCurrentPlayer = 0
  749.     Frame1(glCurrentPlayer).ForeColor = vbHighlight
  750.     LabelScore(glCurrentPlayer).ForeColor = vbHighlight
  751.   
  752.     ' Hide superfluous scoreboxes and initialize scores
  753.     For lCount = 0 To MaxPlayers - 1
  754.         gbPlayerScores(lCount) = 0
  755.         If lCount >= gbNumPlayers Then
  756.             Frame1(lCount).Visible = False
  757.         Else
  758.             Frame1(lCount).Visible = True
  759.             LabelScore(lCount).Caption = 0
  760.         End If
  761.     Next lCount
  762.    
  763.     ' Get names of players and label scoreboxes. The correct order has been
  764.     ' stored in the gPlayerIDs array, which is initialized by the host
  765.     ' and passed to the other players.
  766.     If gbNumPlayers > 1 Then
  767.         For lCount = 0 To gbNumPlayers - 1
  768.             PlayerInfo = dpp.GetPeerInfo(glPlayerIDs(lCount))
  769.             Frame1(lCount).Caption = PlayerInfo.Name
  770.             Frame1(lCount).Tag = glPlayerIDs(lCount)
  771.             If PlayerInfo.lPlayerFlags And DPNPLAYER_LOCAL Then
  772.                 glMyPlayerID = glPlayerIDs(lCount)
  773.             End If
  774.         Next lCount
  775.     End If
  776.     
  777.     ' Erase the pictures and matches
  778.     For lCount = 0 To NumCells - 1
  779.         Image1(lCount).Picture = Nothing
  780.         gfMatchedCells(lCount) = False
  781.     Next lCount
  782.  
  783. End Sub
  784.  
  785. Private Sub tmrResign_Timer()
  786.     tmrResign.Enabled = False
  787.     MsgBox "All other players have resigned.  You win!", vbOKOnly Or vbInformation, "Winner"
  788.     DPlayEventsForm.CloseForm Me
  789. End Sub
  790.  
  791. Public Sub UpdateScoreboard()
  792.  
  793.     Dim lCount As Integer
  794.  
  795.     For lCount = 0 To MaxPlayers - 1
  796.       LabelScore(lCount).Caption = gbPlayerScores(lCount)
  797.     Next lCount
  798.  
  799. End Sub
  800.  
  801. Private Sub UpdateChat(ByVal sText As String, sUser As String)
  802.     'We need to update the chat window
  803.     lblChat.Caption = sUser & " says: " & sText
  804. End Sub
  805.  
  806. Public Sub AdvanceTurn()
  807.   
  808.     If Me.Visible Then
  809.         ' Remove highlight from scorebox for last player
  810.         Frame1(glCurrentPlayer).ForeColor = vbButtonText
  811.         LabelScore(glCurrentPlayer).ForeColor = vbButtonText
  812.     End If
  813.     
  814.     ' Advance the current player. Try till we find one that exists.
  815.     ' Players who resigned are now 0 in gPlayerIDs.
  816.     
  817.     Do
  818.         glCurrentPlayer = glCurrentPlayer + 1
  819.         If glCurrentPlayer = MaxPlayers Then glCurrentPlayer = 0
  820.     Loop Until glPlayerIDs(glCurrentPlayer) <> 0
  821.     
  822.     If Me.Visible Then
  823.         ' Highlight scorebox for active player
  824.         Frame1(glCurrentPlayer).ForeColor = vbHighlight
  825.         LabelScore(glCurrentPlayer).ForeColor = vbHighlight
  826.         UpdateScoreboard
  827.     End If
  828.     Me.Caption = "DirectPlay Memory - " & gsUserName
  829.     If gfHost Then Me.Caption = Me.Caption & " (HOST)"
  830.     If glPlayerIDs(glCurrentPlayer) = glMyPlayerID Then
  831.         Me.Caption = Me.Caption & " - Your turn"
  832.     End If
  833.     
  834. End Sub
  835.  
  836. Private Sub ShowPic(ByVal Index As Integer)
  837.     Dim oBuf() As Byte, lOffset As Long
  838.     ' Show the picture you clicked on
  839.     Image1(Index).Picture = frmPics.Image1(gbPicArray(Index)).Picture
  840.     ' Broadcast message to show picture
  841.     If gbNumPlayers > 1 Then 'NumPlayers > 1
  842.         lOffset = NewBuffer(oBuf)
  843.         AddDataToBuffer oBuf, CByte(MSG_SHOWPIECE), SIZE_BYTE, lOffset
  844.         AddDataToBuffer oBuf, CByte(Index), SIZE_BYTE, lOffset
  845.         SendMessage oBuf
  846.     End If 'NumPlayers > 1
  847. End Sub
  848.  
  849. Private Sub tmrTerminate_Timer()
  850.     tmrTerminate.Enabled = False
  851.     If mfResign Then Exit Sub
  852.     If mlTerminateCode = DPNERR_HOSTTERMINATEDSESSION Then
  853.         MsgBox "The host has terminated this session.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  854.     Else
  855.         MsgBox "This session has been lost.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  856.     End If
  857.     DPlayEventsForm.CloseForm Me
  858. End Sub
  859.  
  860. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  861.     'VB requires that we must implement *every* member of this interface
  862. End Sub
  863.  
  864. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  865.     'VB requires that we must implement *every* member of this interface
  866. End Sub
  867.  
  868. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  869.     'VB requires that we must implement *every* member of this interface
  870. End Sub
  871.  
  872. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  873.     'VB requires that we must implement *every* member of this interface
  874. End Sub
  875.  
  876. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  877.     'VB requires that we must implement *every* member of this interface
  878. End Sub
  879.  
  880. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  881.     gbNumPlayers = gbNumPlayers + 1
  882.     If gbNumPlayers = 1 And mfResign = False Then 'Everyone has resigned, you win!
  883.         tmrResign.Enabled = True
  884.     End If
  885.     ' If current player quit, advance to next
  886.     If glPlayerIDs(glCurrentPlayer) = lPlayerID Then AdvanceTurn
  887. End Sub
  888.  
  889. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  890.     'VB requires that we must implement *every* member of this interface
  891. End Sub
  892.  
  893. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  894.     Dim lCount As Long
  895.     Dim fAdvance As Boolean
  896.     
  897.     On Error Resume Next
  898.     gbNumPlayers = gbNumPlayers - 1
  899.     If gbNumPlayers = 1 And mfResign = False Then 'Everyone has resigned, you win!
  900.         tmrResign.Enabled = True
  901.     End If
  902.     ' If current player quit, advance to next
  903.     If glPlayerIDs(glCurrentPlayer) = lPlayerID Then fAdvance = True
  904.     'Remove this player ID from the list of users
  905.     If gbNumPlayers > 1 Then
  906.         For lCount = 0 To gbNumPlayers + 1
  907.             If Frame1(lCount).Tag = lPlayerID Then
  908.                 Frame1(lCount).Visible = False
  909.             End If
  910.             'Remove this player ID from the list of users
  911.             If glPlayerIDs(lCount) = lPlayerID Then glPlayerIDs(lCount) = 0
  912.         Next lCount
  913.     End If
  914.     
  915.     If fAdvance Then AdvanceTurn
  916. End Sub
  917.  
  918. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  919.     'We don't want anyone to see this game once it's started... Disallow it.
  920.     fRejectMsg = True
  921. End Sub
  922.  
  923. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  924.     'VB requires that we must implement *every* member of this interface
  925. End Sub
  926.  
  927. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  928.     If lNewHostID = glMyPlayerID Then gfHost = True
  929. End Sub
  930.  
  931. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  932.     'We don't want anyone connecting while we're already playing the game.. Disallow it.
  933.     fRejectMsg = True
  934. End Sub
  935.  
  936. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  937.     'VB requires that we must implement *every* member of this interface
  938. End Sub
  939.  
  940. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  941.     'VB requires that we must implement *every* member of this interface
  942. End Sub
  943.  
  944. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  945.     
  946.     Dim lCount As Long, lOffset As Long
  947.     Dim bMsg As Byte
  948.     Dim bPiece As Byte, fMatched As Boolean, bScore As Byte
  949.     Dim sChat As String, sPlayer As String
  950.     
  951.     'Here we will go through the messages
  952.     'The first item in our byte array is the MSGID we passed in
  953.     With dpnotify
  954.     GetDataFromBuffer .ReceivedData, bMsg, LenB(bMsg), lOffset
  955.     Select Case bMsg
  956.     Case MSG_SHOWPIECE
  957.         ' Show a tile that has been clicked
  958.         GetDataFromBuffer .ReceivedData, bPiece, LenB(bPiece), lOffset
  959.         frmGameBoard.Image1(bPiece).Picture = frmPics.Image1(gbPicArray(bPiece)).Picture
  960.       
  961.     Case MSG_HIDEPIECES
  962.         ' Hide unmatched pieces because player has made the first pick.
  963.         For lCount = 0 To NumCells - 1
  964.             If Not gfMatchedCells(lCount) Then
  965.                 Image1(lCount).Picture = Nothing
  966.             End If
  967.         Next lCount
  968.     
  969.     Case MSG_MATCHED
  970.     ' Retrieve matched cells array
  971.         For lCount = 0 To NumCells - 1
  972.             GetDataFromBuffer .ReceivedData, fMatched, LenB(fMatched), lOffset
  973.             gfMatchedCells(lCount) = fMatched
  974.         Next lCount
  975.         
  976.         ' Retrieve player scores array
  977.         For lCount = 0 To MaxPlayers - 1
  978.             GetDataFromBuffer .ReceivedData, bScore, LenB(bScore), lOffset
  979.             gbPlayerScores(lCount) = bScore
  980.         Next lCount
  981.         ' Display current score
  982.         frmGameBoard.UpdateScoreboard
  983.     
  984.     Case MSG_TURNEND
  985.         AdvanceTurn
  986.     
  987.     Case MSG_CHAT
  988.     ' Display chat message
  989.         sPlayer = dpp.GetPeerInfo(dpnotify.idSender).Name
  990.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  991.         UpdateChat sChat, sPlayer
  992.     End Select
  993.     End With
  994.     
  995. End Sub
  996.  
  997. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  998.     'VB requires that we must implement *every* member of this interface
  999. End Sub
  1000.  
  1001. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  1002.     mlTerminateCode = dpnotify.hResultCode
  1003.     tmrTerminate.Enabled = True
  1004. End Sub
  1005.  
  1006.